//
//  YSCreateDB.m
//  Yaoyle
//
//  Created by ZhaoXudong on 14-9-23.
//  Copyright (c) 2014年 ZhaoXudong. All rights reserved.
//

#import "YSCreateDB.h"

#define kUserTableName @"usertable"

@implementation YSCreateDB

//创建数据库表
+ (void)createDataBaseTable
{
    FMDatabase *dataBase = [[YSDBManage shareManage] dataBase];
    if (![dataBase tableExists:kUserTableName]) {
        
        NSString * sql = [NSString stringWithFormat:@"CREATE TABLE %@ (phone text primary key not null, password text)", kUserTableName];
        BOOL result = [dataBase executeUpdate:sql];
        if (!result) {
            NSLog(@"数据表创建失败");
        }
    }
}

+ (BOOL)insertUserInfo:(NSString *)phone andPwd:(NSString *)psd {
    [YSCreateDB createDataBaseTable];
    NSString *sql = [NSString stringWithFormat:@"insert into %@ (phone,password) values ('%@','%@')",kUserTableName, phone, psd];
    return [[YSDBManage shareManage].dataBase executeUpdate:sql];
}

+ (BOOL)updateUserInfo:(NSString *)phone andPwd:(NSString *)psd {
    NSString *sql = [NSString stringWithFormat:@"UPDATE %@ SET password = '%@' WHERE phone = '%@'",kUserTableName, psd, phone];
    return [[YSDBManage shareManage].dataBase executeUpdate:sql];
}
//
+ (FMResultSet *)queryUser:(NSString *)phone {
    NSString *sql = [NSString stringWithFormat:@"select * from %@ where phone = '%@'",kUserTableName,phone];
    FMResultSet * result = [[YSDBManage shareManage].dataBase executeQuery:sql];
    return result;

}
//
+ (BOOL)deleteUser:(NSString *)phone {
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM %@ WHERE phone = %@", kUserTableName, phone];
    return [[YSDBManage shareManage].dataBase executeUpdate:sql];
}

@end
